// 处理数据库
const {
	dbQuery
} = require('../db/index');
// 处理验证
const joi = require('joi');
// 处理加密
const bcrypt = require('bcryptjs');
// 处理响应函数
const {
	sendError,
	sendSuccess
} = require('../tools/sendFunction.js')

// 设定验证规则
var guiZe = joi.object({
	id: joi.number().min(1).required().error(new Error('id不符合验证规则')),
	nickname: joi.string().required().error(new Error('nickname不符合验证规则')),
	email: joi.string().email().required().error(new Error('email不符合验证规则')),
})

var guiZe2 = joi.object({
	oldPwd: joi.string().required().regex(/^[\S]{6,12}$/).error(new Error('旧密码不符合验证规则')),
	newPwd: joi.string().required().regex(/^[\S]{6,12}$/).error(new Error('新密码不符合验证规则')),
})

var guiZe3 = joi.object({
	avatar: joi.string().dataUri().required().error(new Error('avatar不符合验证规则'))
})


// 获取用户信息
exports.getUserInfo = async (req, res) => {
	try {
		var result = await dbQuery('select id, username, nickname, email, user_pic from users where id =?', [req.user.id])
	} catch (error) {
		next(error);
		return;
	}
	if (result.length == 0) {
		sendError(res, 200, '获取用户信息失败!')
	} else {
		sendSuccess(res, '获取用户基本信息成功!', {
			data: result[0]
		})
	}
}

// 更新用户信息
exports.updateUserInfo = async (req, res, next) => {
	let userInfo = req.body;

	// 验证请求参数
	try {
		await guiZe.validateAsync({
			id: userInfo.id,
			nickname: userInfo.nickname,
			email: userInfo.email
		})
	} catch (error) {
		next(error);
		return;
	}

	// 更新
	try {
		var result = await dbQuery('update users set nickname=?,email=? where id=?', [userInfo.nickname, userInfo.email, userInfo.id])
	} catch (error) {
		next(error);
		return;
	}

	// 更新成功
	if (result.affectedRows == 1) {
		sendSuccess(res, '修改用户基本信息成功!');
	} else {
		// 更新失败
		sendError(res, 200, '修改用户基本信息失败!');
	}
}

// 重置密码
exports.updatepwd = async (req, res, next) => {
	let {
		oldPwd,
		newPwd
	} = req.body;
	let id = req.user.id;

	// 验证请求参数
	try {
		await guiZe2.validateAsync({
			oldPwd: oldPwd,
			newPwd: newPwd
		})
	} catch (error) {
		next(error)
		return
	}

	// 验证通过
	try {
		var result = await dbQuery('select password from users where id=?', [id])
	} catch (error) {
		next(error)
		return;
	}

	// 验证旧密码
	let isValid = await bcrypt.compare(oldPwd, result[0].password)
	// 旧密码正确
	if (isValid) {
		let salt = await bcrypt.genSalt(10);
		newPwd = await bcrypt.hash(newPwd, salt);
		// 更新
		try {
			var result2 = await dbQuery('update users set password=? where id =?', [newPwd, id])
		} catch (error) {
			next(error)
			return;
		}
		if (result2.affectedRows == 1) {
			sendSuccess(res, '更新密码成功!');
		} else {
			sendError(res, 200, '更新密码失败!');
		}
	} else {
		sendError(res, 200, '原密码错误！')
	}
}

exports.updateAvatar = async (req, res, next) => {
	let avatarUrl = req.body.avatar;
	let id = req.user.id;

	// 验证请求参数
	try {
		await guiZe3.validateAsync({
			avatar: avatarUrl
		})
	} catch (error) {
		next(error);
		return;
	}

	// 请求参数验证通过
	// 更新
	try {
		var result = await dbQuery('update users set user_pic =? where id=?', [avatarUrl, id])
	} catch (error) {
		next(error);
		return
	}
	if (result.affectedRows == 1) {
		console.log(result)
		sendSuccess(res, '更新头像成功!')
	} else {
		sendError(res, 200, '更新头像失败!')
	}
}